package com.dankegongyu.lib.common.base;

import android.content.Intent;
import android.os.Bundle;

import com.dankegongyu.lib.common.annotation.BindEventBus;
import com.dankegongyu.lib.common.event.DKEventBus;
import com.dankegongyu.lib.common.rx.presenter.IPresenter;
import com.dankegongyu.lib.common.util.DKMediaUtil;
import com.dankegongyu.lib.common.util.DKRouter;
import com.dankegongyu.lib.common.util.DKSharedElementUtil;
import com.dankegongyu.lib.common.util.fresco.FrescoImageHelper;
import com.dankegongyu.lib.common.util.statusbar.DKStatusBarConfig;
import com.dankegongyu.lib.common.util.statusbar.DKStatusBarMode;
import com.dankegongyu.lib.common.util.statusbar.DKStatusBarUtils;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;

/**
 * BaseActivity更名为CommonBaseActivity，不建议app module中的Activity直接继承BaseActivity，
 * 建议中间加一层BaseAppActivity继承BaseActivity，方便扩展
 *
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/13 18:42
 */
public abstract class CommonBaseActivity extends RxAppCompatActivity implements IBaseActivity {

    protected List<IPresenter> mPresenterList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        DKSharedElementUtil.init(this);
        super.onCreate(savedInstanceState);
        DKSharedElementUtil.setCallback(this);

        DKRouter.inject(this);

        setContentView(getContentViewLayoutId());
        ButterKnife.bind(this);

        if (getClass().isAnnotationPresent(BindEventBus.class)) {
            DKEventBus.register(this);
        }

        setCustomStatusBar();
        init(savedInstanceState);

        initPresenter();
        for (IPresenter p : mPresenterList) {
            if (p != null) {
                p.onCreate();
            }
        }

        doBusiness();

    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 1.EventBus解注册
        if (getClass().isAnnotationPresent(BindEventBus.class)) {
            DKEventBus.unregister(this);
        }

        // 2.Presenter销毁
        for (IPresenter p : mPresenterList) {
            if (p != null) {
                p.onDestroy();
            }
        }

        // 拍照或裁剪
        DKMediaUtil.release();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        DKRouter.inject(this);
    }

    @Override
    public void onBackPressed() {
        // 解决java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        try {
            super.onBackPressed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否自定义状态栏
     */
    protected boolean useCustomStatusBar() {
        return DKStatusBarConfig.getInstance().useCustomStatusBar();
    }

    /**
     * 自定义状态栏
     */
    protected void setCustomStatusBar() {
        DKStatusBarConfig statusBarConfig = DKStatusBarConfig.getInstance();
        if (useCustomStatusBar()) {
            // 状态栏颜色
            if (statusBarConfig.getStatusBarColor() > 0) {
                DKStatusBarUtils.setColor(this, statusBarConfig.getStatusBarColor(), 0);
            }
            // 状态栏模式
            if (statusBarConfig.getStatusBarMode() == DKStatusBarMode.LIGHT) {
                // SwipeBack也需要处理，必须在设置状态栏模式之前
                DKStatusBarUtils.setColorForSwipeBackLightMode(this, statusBarConfig.getStatusBarColor(), 0);
//                DKStatusBarUtils.setTransparentLightMode(this);
            } else if (statusBarConfig.getStatusBarMode() == DKStatusBarMode.DARK) {
                // SwipeBack也需要处理，必须在设置状态栏模式之前
                DKStatusBarUtils.setColorForSwipeBackDarkMode(this, statusBarConfig.getStatusBarColor(), 0);
//                DKStatusBarUtils.setTransparentDarkMode(this);
            }
        }
    }

    /**
     * 设置状态栏颜色
     *
     * @param color color res id
     */
    protected void setStatusBarColor(@DKStatusBarMode int mode, @ColorRes int color) {
        setCustomStatusBar(); // 先调用默认设置
        DKStatusBarUtils.setColor(this, color, 0);
        if (mode == DKStatusBarMode.LIGHT) {
            DKStatusBarUtils.setColorForSwipeBackLightMode(this, color, 0);
        } else if (mode == DKStatusBarMode.DARK) {
            DKStatusBarUtils.setColorForSwipeBackDarkMode(this, color, 0);
        }
    }

    protected void addPresenter(IPresenter presenter) {
        if (presenter != null) {
            mPresenterList.add(presenter);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        FrescoImageHelper.clearMemoryCaches();
    }

}
